import 'package:flutter_test/flutter_test.dart';
import 'package:logger/logger.dart';
import '../lib/services/system_settings_service.dart';
import '../lib/core/database_helper.dart';

void main() {
  group('SystemSettingsService Tests', () {
    late SystemSettingsService settingsService;
    late DatabaseHelper databaseHelper;

    setUpAll(() async {
      // 初始化数据库
      await DatabaseHelper.initializeDatabaseFactory();
      databaseHelper = DatabaseHelper();
      await databaseHelper.database;
      
      settingsService = SystemSettingsService();
    });

    group('getSystemInfo', () {
      test('should return system info with correct structure', () async {
        final result = await settingsService.getSystemInfo();
        
        expect(result, isA<Map<String, dynamic>>());
        expect(result['app_name'], isA<String>());
        expect(result['app_version'], isA<String>());
        expect(result['database_path'], isA<String>());
        expect(result['database_size'], isA<int>());
        expect(result['database_version'], isA<int>());
        expect(result['record_count'], isA<int>());
        expect(result['user_count'], isA<int>());
        expect(result['system_uptime'], isA<int>());
      });

      test('should handle system info retrieval', () async {
        final result = await settingsService.getSystemInfo();
        
        expect(result['app_name'], isNotEmpty);
        expect(result['app_version'], isNotEmpty);
        expect(result['database_path'], isNotEmpty);
        expect(result['database_size'], greaterThanOrEqualTo(0));
        expect(result['database_version'], greaterThanOrEqualTo(0));
        expect(result['record_count'], greaterThanOrEqualTo(0));
        expect(result['user_count'], greaterThanOrEqualTo(0));
      });
    });

    group('getSystemStatistics', () {
      test('should return system statistics with correct structure', () async {
        final result = await settingsService.getSystemStatistics();
        
        expect(result, isA<Map<String, dynamic>>());
        expect(result['user_statistics'], isA<Map<String, dynamic>>());
        expect(result['record_statistics'], isA<Map<String, dynamic>>());
        expect(result['process_statistics'], isA<List>());
        
        // 检查用户统计
        final userStats = result['user_statistics'] as Map<String, dynamic>;
        expect(userStats['total_users'], isA<int>());
        expect(userStats['active_users'], isA<int>());
        expect(userStats['admin_users'], isA<int>());
        
        // 检查记录统计
        final recordStats = result['record_statistics'] as Map<String, dynamic>;
        expect(recordStats['total_records'], isA<int>());
        expect(recordStats['recent_records'], isA<int>());
        
        // 检查工艺统计
        final processStats = result['process_statistics'] as List;
        expect(processStats, isA<List>());
      });

      test('should handle empty database statistics', () async {
        final result = await settingsService.getSystemStatistics();
        
        final userStats = result['user_statistics'] as Map<String, dynamic>;
        expect(userStats['total_users'], greaterThanOrEqualTo(0));
        expect(userStats['active_users'], greaterThanOrEqualTo(0));
        expect(userStats['admin_users'], greaterThanOrEqualTo(0));
        
        final recordStats = result['record_statistics'] as Map<String, dynamic>;
        expect(recordStats['total_records'], greaterThanOrEqualTo(0));
        expect(recordStats['recent_records'], greaterThanOrEqualTo(0));
      });
    });

    group('getBackupList', () {
      test('should return backup list with correct structure', () async {
        final result = await settingsService.getBackupList();
        
        expect(result, isA<List<Map<String, dynamic>>>());
        
        if (result.isNotEmpty) {
          final backup = result.first;
          expect(backup['file_path'], isA<String>());
          expect(backup['file_name'], isA<String>());
          expect(backup['file_size'], isA<int>());
          expect(backup['created_time'], isA<DateTime>());
          expect(backup['timestamp'], isA<int>());
        }
      });

      test('should handle empty backup list', () async {
        final result = await settingsService.getBackupList();
        expect(result, isA<List>());
        // 初始状态下可能没有备份文件
      });
    });

    group('validateBackup', () {
      test('should validate backup file structure', () async {
        // 这个测试需要实际的备份文件
        // 在没有备份文件的情况下，应该返回false
        final result = await settingsService.validateBackup('nonexistent.db');
        expect(result, isFalse);
      });
    });

    group('cleanupOldBackups', () {
      test('should cleanup old backups', () async {
        final result = await settingsService.cleanupOldBackups(keepCount: 5);
        
        expect(result, isA<int>());
        expect(result, greaterThanOrEqualTo(0));
      });

      test('should handle cleanup with no old backups', () async {
        final result = await settingsService.cleanupOldBackups(keepCount: 10);
        
        expect(result, isA<int>());
        expect(result, greaterThanOrEqualTo(0));
      });
    });

    group('Error Handling', () {
      test('should handle database errors gracefully', () async {
        // 这个测试验证服务能够处理数据库错误
        expect(
          () => settingsService.getSystemInfo(),
          returnsNormally,
        );
      });

      test('should handle backup operations errors gracefully', () async {
        // 测试备份操作的错误处理
        expect(
          () => settingsService.getBackupList(),
          returnsNormally,
        );
      });
    });

    group('Database Path', () {
      test('should get database path successfully', () async {
        final dbPath = await databaseHelper.databasePath;
        
        expect(dbPath, isA<String>());
        expect(dbPath.isNotEmpty, isTrue);
        expect(dbPath.endsWith('.db'), isTrue);
      });
    });
  });
} 